package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---添加银行卡
 */
public class AddBankCard extends BaseActivity implements View.OnClickListener {
    TextView title;
    private static final int SET_ADDRESS_REQUESTCODE = 540;
    private static final int SET_BANK_REQUESTCODE = 5550;
    private String province;
    private String city;
    private String area;
    private TextView address;
    private EditText name_ed, card_number, bank_branch,idCard_ed;
    private TextView bank_mame_tv;
    final ArrayList<String> list = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    startActivityForResult(new Intent(AddBankCard.this, SetBankActivity.class).putExtra("list", list), SET_BANK_REQUESTCODE);
                    overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
                    break;

                default:
                    break;
            }

            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card);
        initTirle();
        address = (TextView) findViewById(R.id.address);
        name_ed = (EditText) findViewById(R.id.name_ed);
        card_number = (EditText) findViewById(R.id.card_number);
        bank_branch = (EditText) findViewById(R.id.bank_branch);
        idCard_ed = (EditText) findViewById(R.id.idCard_ed);
        bank_mame_tv = (TextView) findViewById(R.id.bank_mame);
        findViewById(R.id.address_lay).setOnClickListener(this);
        findViewById(R.id.bank_mame_lay).setOnClickListener(this);
        findViewById(R.id.btn_add_bank_card).setOnClickListener(this);

        name_ed.setFocusable(true);
        name_ed.setFocusableInTouchMode(true);
        name_ed.requestFocus();
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("添加银行卡");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    List<String> num = new ArrayList<>();
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            case R.id.address_lay:
                startActivityForResult(new Intent(this, SetAdressActivity.class), SET_ADDRESS_REQUESTCODE);
                overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
                break;

            case R.id.bank_mame_lay:

                showDialog();
                XUtil.Post(ConfigUtil.BANKLIST, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        System.out.println("bank_mame_lay" + result);
                        try {
                            list.clear();
                            num.clear();
                            JSONObject jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
                            JSONArray jsonArray = jsonObject.optJSONArray("info");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                jsonObject = jsonArray.optJSONObject(i);
                                String id = jsonObject.optString("id");
                                String name = jsonObject.optString("name");
                                num.add(id);
                                list.add(name);
                            }
                            mHandler.sendEmptyMessage(1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                        closeDialog();
                    }
                });



                break;

            //添加
            case R.id.btn_add_bank_card:
                uploadByxUtils();
                break;
        }
    }

    private static final String TAG = "AddBankCard";
    private String idCard;

    private void uploadByxUtils() {
        String bankName = name_ed.getText().toString();
        String bankCard = card_number.getText().toString();
//        String bankAddr = address.getText().toString();
        String bankId = null;
        if (index != -1){

            bankId = num.get(index);
        }
        String bankBranck = bank_branch.getText().toString();

        idCard = idCard_ed.getText().toString();
        if (bankName == null|| "".equals(bankName)){
            MyToast("请输入开户名");
        }else if (!MyUtils.isIDCard(idCard)){
            MyToast("请输入正确身份证号");
        }else if (bankCard == null ||"".equals(bankCard)){
            MyToast("请输入银行卡号");
        }else if (bankId == null || "".equals(bankId)){
            MyToast("请选择开户银行");
        }else if (bankBranck == null || "".equals(bankBranck)){
            MyToast("请输入开户支行");
        }else {
            addCard(bankName, bankCard, bankId, bankBranck);
        }

    }

    private void addCard(String bankName, String bankCard, String bankId, String bankBranck) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("bankCardNumber", bankCard);//银行卡号
        map.put("name", bankName);//开户用户名
//        map.put("address", bankAddr);//开户所在地
        map.put("bank", bankId);//开户行id
        map.put("bankBranck", bankBranck);//支行
        map.put("idCard", idCard);//支行

        showDialog();
        XUtil.Post(ConfigUtil.ADDBANK, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: AddBankCard" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");

                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    String bank_name;
    int index = -1;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SET_ADDRESS_REQUESTCODE) {
            if (resultCode == RESULT_OK) {
                province = data.getStringExtra("province");
                city = data.getStringExtra("city");
                area = data.getStringExtra("area");
                address.setText(city + "-" + area);
            }
        } else if (requestCode == SET_BANK_REQUESTCODE) {
            if (resultCode == RESULT_OK) {
                bank_name = data.getStringExtra("bank_name");
                index = data.getIntExtra("index",0);
                bank_mame_tv.setText(bank_name);
            }
        }
    }


}
